package com.hengxinguotong.zhihuichengjian.customtimepicker;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.customtimepicker.adapter.MonthTimeAdapter;
import com.hengxinguotong.zhihuichengjian.customtimepicker.bean.DayTimeEntity;
import com.hengxinguotong.zhihuichengjian.customtimepicker.bean.MonthTimeEntity;
import com.hengxinguotong.zhihuichengjian.customtimepicker.bean.UpdataCalendar;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.ButterKnife;


public class TimePickerActivity extends TimeActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private HXTextView titleTv;
    private HXTextView returnBack;
    private HXTextView commitTv;
    private static HXTextView todayYv;
    private static HXTextView toweekTv;
    private static HXTextView tomonthTv;
    private static HXTextView toyearTv;
    private HXTextView plan_time_txt_month;
    private RecyclerView reycycler;

    private MonthTimeAdapter adapter;
    private ArrayList<MonthTimeEntity> datas;

    private int mSuspensionHeight;
    private int mCurrentPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picker_time);
        ButterKnife.bind(this);
        initView();
        initData();

        EventBus.getDefault().register(this);

    }

    private void initData() {
        startDay = new DayTimeEntity(0,0,0,0);
        stopDay = new DayTimeEntity(-1,-1,-1,-1);

        datas = new ArrayList<>();

        Calendar c = Calendar.getInstance();

        today = c.get(Calendar.DAY_OF_MONTH);

        c.add(Calendar.MONTH,1);
        c.add(Calendar.YEAR, -backward);
        int nextYear = c.get(Calendar.YEAR);
        int nextMonth = c.get(Calendar.MONTH);

        for (int i = 0; i < 60; i++) {
            datas.add(new MonthTimeEntity(nextYear,nextMonth,nextYear + "--"+ nextMonth));
            if (nextMonth == 12){
                nextMonth = 0;
                nextYear = nextYear + 1;
            }
            nextMonth = nextMonth + 1;
        }

        adapter = new MonthTimeAdapter(datas, TimePickerActivity.this);
        reycycler.setAdapter(adapter);
        reycycler.scrollToPosition(backward * 12);

    }

    private void initView() {
        titleTv = (HXTextView) findViewById(R.id.title_tv);
        returnBack = (HXTextView) findViewById(R.id.return_back);
        returnBack.setOnClickListener(this);
        commitTv = (HXTextView) findViewById(R.id.commit_tv);
        commitTv.setOnClickListener(this);
        todayYv = (HXTextView) findViewById(R.id.today_tv);
        todayYv.setOnClickListener(this);
        toweekTv = (HXTextView) findViewById(R.id.toweek_tv);
        toweekTv.setOnClickListener(this);
        tomonthTv = (HXTextView) findViewById(R.id.tomonth_tv);
        tomonthTv.setOnClickListener(this);
        toyearTv = (HXTextView) findViewById(R.id.toyear_tv);
        toyearTv.setOnClickListener(this);
        plan_time_txt_month = (HXTextView) findViewById(R.id.plan_time_txt_month);
        reycycler = (RecyclerView) findViewById(R.id.plan_time_calender);

        final LinearLayoutManager layoutManager =
                new LinearLayoutManager(this,   // 上下文
                        LinearLayout.VERTICAL,  //垂直布局,
                        false);

        reycycler.setLayoutManager(layoutManager);

        reycycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                mSuspensionHeight = plan_time_txt_month.getHeight();
            }

            @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = layoutManager.findViewByPosition(mCurrentPosition + 1);
                if (view != null) {
                    if (view.getTop() <= mSuspensionHeight) {
                        plan_time_txt_month.setY(-(mSuspensionHeight - view.getTop()));
                    } else {
                        plan_time_txt_month.setY(0);
                    }
                }

                if (mCurrentPosition != layoutManager.findFirstVisibleItemPosition()) {
                    mCurrentPosition = layoutManager.findFirstVisibleItemPosition();
                    plan_time_txt_month.setY(0);
                    plan_time_txt_month.setText(datas.get(mCurrentPosition).getSticky());
                }
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(UpdataCalendar event) {
        adapter.notifyDataSetChanged();
        if(startDay.getDay() > 0 && stopDay.getDay() > 0){
            exit();
        }else {
            showToast("请选择时间段");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private DayTimeEntity dateformat(Calendar calendar){
        DayTimeEntity dayTimeEntity = new DayTimeEntity(calendar.get(Calendar.DAY_OF_MONTH),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.YEAR));
        return dayTimeEntity;
    }

    @Override
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance();
        switch (v.getId()){
            case R.id.return_back:
                finish();
                break;
            case R.id.today_tv:
                clearState();
                todayYv.setEnabled(false);
                startDay = dateformat(calendar);
                stopDay = dateformat(calendar);
                exit();
                break;
            case R.id.toweek_tv:
                clearState();
                toweekTv.setEnabled(false);
                calendar.set(Calendar.DAY_OF_WEEK, 1);
                startDay = dateformat(calendar);
                calendar.set(Calendar.DAY_OF_WEEK, 7);
                stopDay = dateformat(calendar);
                exit();
                break;
            case R.id.tomonth_tv:
                clearState();
                tomonthTv.setEnabled(false);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                startDay = dateformat(calendar);
                calendar.add(Calendar.MONTH, 1);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                stopDay = dateformat(calendar);
                exit();
                break;
            case R.id.toyear_tv:
                clearState();
                toyearTv.setEnabled(false);
                calendar.set(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                startDay = dateformat(calendar);
                calendar.add(Calendar.YEAR, 1);
                calendar.add(Calendar.DAY_OF_YEAR, -1);
                stopDay = dateformat(calendar);
                exit();
                break;
            case R.id.commit_tv:
                if(startDay.getDay() > 0 && stopDay.getDay() > 0){
                    exit();
                }else {
                    showToast("请选择时间段");
                }
                break;
        }
    }

    private void exit(){
        Intent intent = getIntent();
        intent.putExtra("startDay", startDay);
        intent.putExtra("stopDay", stopDay);
        setResult(RESULT_OK, intent);
        finish();
    }

    public static void clearState(){
        todayYv.setEnabled(true);
        toweekTv.setEnabled(true);
        tomonthTv.setEnabled(true);
        toyearTv.setEnabled(true);
    }
}
